﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace EssenceFeed.Core.Helpers
{
    /// <summary>
    /// String Extensions
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// Truncate String
        /// </summary>
        /// <param name="input"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static string Truncate(string input, int maxLength)
        {
            if (!string.IsNullOrEmpty(input) && maxLength > 0)
            {
                return input.Length > maxLength ? input.Substring(0, maxLength) + "..." : input;
            }
            return string.Empty;
        }

        /// <summary>
        /// IsNumeric Function
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static bool IsNumeric(string expression)
        {
            double retNum;
            var isNum = Double.TryParse(expression, System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
            return isNum;
        }

        /// <summary>
        /// Split the text into array
        /// </summary>
        /// <param name="textToSplit">String to be Splited</param>
        /// <param name="splitter">Separator character</param>
        /// <returns></returns>
        public static string[] SplitText(string textToSplit, string splitter)
        {
            if (textToSplit.IndexOf(splitter) != -1 && splitter != string.Empty)
            {
                var count = 0;
                var temp = textToSplit;
                var count2 = splitter.Length;
                while (temp.IndexOf(splitter) != -1)
                {
                    var seq = temp.IndexOf(splitter);
                    var top = seq + count2;
                    temp = temp.Remove(0, top);
                    count++;
                }
                var splittedText = new string[count + 1];
                var i = 0;
                while (textToSplit.IndexOf(splitter) != -1)
                {
                    var seq = textToSplit.IndexOf(splitter);
                    splittedText[i] = textToSplit.Substring(0, seq);
                    textToSplit = textToSplit.Remove(0, (seq + count2));
                    i++;
                }
                splittedText[count] = textToSplit;
                return splittedText;
            }
            return new string[] { textToSplit};
        }

        /// <summary>
        /// Replace String
        /// </summary>
        /// <param name="strInput"></param>
        /// <param name="strReplaced"></param>
        /// <param name="strReplacing"></param>
        /// <returns></returns>
        private static string replaceString(string strInput, string strReplaced, string strReplacing)
        {
            return Regex.Replace(strInput, strReplaced, strReplacing);
        }

        /// <summary>
        /// Replace String from Sorted List
        /// </summary>
        /// <param name="strBody"></param>
        /// <param name="slReplace"></param>
        /// <returns></returns>
        public static string ReplaceStringFromList(string strBody,SortedList slReplace)
        {
            foreach (string key in slReplace.Keys)
            {
                strBody = replaceString(strBody, key, slReplace[key].ToString());
            }
            return strBody;
        }

        /// <summary>
        /// Compute Hash
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ComputeHash(this string value)
        {
            var hash = value;

            if (value != null)
            {
                var md5 = new MD5CryptoServiceProvider();
                var data = Encoding.ASCII.GetBytes(value);
                data = md5.ComputeHash(data);
                hash = "";
                for (var i = 0; i < data.Length; i++)
                {
                    hash += data[i].ToString("x2");
                }
            }
            return hash;
        }

        /// <summary>
        /// Generate Random String
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GenerateRandomString(int length)
        {
            RandomNumberGenerator cryptoRandomDataGenerator = new RNGCryptoServiceProvider();
            var buffer = new byte[length];
            cryptoRandomDataGenerator.GetBytes(buffer);
            return Convert.ToBase64String(buffer);
        }
    }
}